package com.zoe.thread.th10_BlockingQueue阻塞队列;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Author Zoe
 * @Date: 2021/05/04
 * @Description 阻塞队列
 */
public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
//        异常
//        System.out.println(queue.add("a"));
//        System.out.println(queue.add("b"));
//        System.out.println(queue.add("c"));
//        System.out.println(queue.remove()+"：移除元素");
//        queue.forEach(System.out::println);

//        特殊值
//        System.out.println(queue.offer("a"));
//        System.out.println(queue.offer("b"));
//        System.out.println(queue.offer("c"));
//        System.out.println(queue.element()+" 输出一个元素;不删除");
//        queue.forEach(System.out::println);


//        System.out.println(queue.offer("a"));
//        System.out.println(queue.offer("b"));
//        System.out.println(queue.offer("c"));
//        System.out.println(queue.poll() + " 移除元素");
//        System.out.println(queue.poll() + " 移除元素");
//        queue.forEach(System.out::println);

//        阻塞
//        queue.put("a");
//        queue.put("b");
//        queue.put("c");
//        queue.take();
//        queue.put("q");
//        queue.forEach(System.out::println);

//        超时自动退出
        System.out.println(queue.offer("a", 3L, TimeUnit.SECONDS));
        System.out.println(queue.offer("b", 3L, TimeUnit.SECONDS));
        System.out.println(queue.offer("c", 3L, TimeUnit.SECONDS));
        System.out.println(queue.offer("d", 3L, TimeUnit.SECONDS));
    }
}
